from django.db import models


class Banner(models.Model):
    """轮播图模型"""
    image_url = models.URLField(verbose_name='图片URL')
    link = models.CharField(max_length=200, verbose_name='跳转链接')
    sort = models.IntegerField(default=0, verbose_name='排序')
    created_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_time = models.DateTimeField(auto_now=True, verbose_name='更新时间')

    class Meta:
        verbose_name = '轮播图'
        verbose_name_plural = verbose_name
        ordering = ['sort', '-created_time']

    def __str__(self):
        return f'Banner {self.id}'


class Category(models.Model):
    """分类模型"""
    name = models.CharField(max_length=50, verbose_name='分类名称')
    icon_url = models.URLField(verbose_name='图标URL')
    parent = models.ForeignKey('self', null=True, blank=True, on_delete=models.CASCADE, related_name='sub_categories', verbose_name='父分类')
    created_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_time = models.DateTimeField(auto_now=True, verbose_name='更新时间')

    class Meta:
        verbose_name = '分类'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class Goods(models.Model):
    """商品模型"""
    name = models.CharField(max_length=100, verbose_name='商品名称')
    image_url = models.URLField(verbose_name='商品图片URL')
    original_price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='原价')
    current_price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='现价')
    sales = models.IntegerField(default=0, verbose_name='销量')
    is_hot = models.BooleanField(default=False, verbose_name='是否爆款')
    category = models.ForeignKey(Category, on_delete=models.CASCADE, related_name='goods', verbose_name='分类')
    spec = models.CharField(max_length=50, verbose_name='规格', null=True, blank=True)
    limit_buy = models.BooleanField(default=False, verbose_name='是否限购')
    created_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_time = models.DateTimeField(auto_now=True, verbose_name='更新时间')

    class Meta:
        verbose_name = '商品'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class Cart(models.Model):
    """购物车模型"""
    user = models.ForeignKey('auth.User', on_delete=models.CASCADE, related_name='carts', verbose_name='用户')
    goods = models.ForeignKey(Goods, on_delete=models.CASCADE, related_name='carts', verbose_name='商品')
    num = models.IntegerField(default=1, verbose_name='数量')
    spec = models.CharField(max_length=50, verbose_name='规格')
    selected = models.BooleanField(default=True, verbose_name='是否选中')
    add_time = models.DateTimeField(auto_now_add=True, verbose_name='添加时间')

    class Meta:
        verbose_name = '购物车'
        verbose_name_plural = verbose_name
        unique_together = ('user', 'goods', 'spec')

    def __str__(self):
        return f'{self.user.username} - {self.goods.name} x {self.num}'